<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="公路编码" prop="field1">
        <el-input
          v-model="queryParams.field1"
          placeholder="请输入公路编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公路名称" prop="field2">
        <el-input
          v-model="queryParams.field2"
          placeholder="请输入公路名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:gonglu:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          v-if="false"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:gonglu:edit']"
          >编辑/查看</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          v-if="false"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:gonglu:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          v-if="false"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['biz:gonglu:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="gongluList"
      @selection-change="handleSelectionChange"
    >
<!--      <el-table-column label="公路ID" align="center" prop="id" />-->
      <el-table-column label="公路编码" align="center" prop="field1" />
      <el-table-column label="公路名称" align="center" prop="field2" />
      <el-table-column label="公路方向" align="center" prop="field3" />
      <el-table-column label="公路起点" align="center" prop="field4" />
      <el-table-column label="公路终点" align="center" prop="field5" />
<!--      <el-table-column label="设计单位" align="center" prop="field6" />-->
<!--      <el-table-column label="施工单位" align="center" prop="field7" />-->
<!--      <el-table-column label="管理单位" align="center" prop="field8" />-->
      <el-table-column label="公路等级" align="center" prop="field9"></el-table-column>
<!--      <el-table-column label="设计时速" align="center" prop="field10" />-->
      <el-table-column label="公路长度" align="center" prop="field11" />
      <el-table-column
        label="操作"
        fixed="right"
        width="200px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:gonglu:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:gonglu:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑/查看公路信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="55%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公路编码" prop="field1">
              <el-input v-model="form.field1" placeholder="请输入公路编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公路名称" prop="field2">
              <el-input v-model="form.field2" placeholder="请输入公路名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公路方向" prop="field3">
              <el-input v-model="form.field3" placeholder="请输入公路方向" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公路起点" prop="field4">
              <el-input v-model="form.field4" placeholder="请输入公路起点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公路终点" prop="field5">
              <el-input v-model="form.field5" placeholder="请输入公路终点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公路长度" prop="field11">
              <el-input v-model="form.field11" placeholder="请输入公路长度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起点经度" prop="field20">
              <el-input-number controls-position="right" v-model="form.field20" placeholder="请输入终点经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="起点纬度" prop="field21">
              <el-input-number controls-position="right" v-model="form.field21" placeholder="请输入终点纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="终点经度" prop="field22">
              <el-input-number controls-position="right" v-model="form.field22" placeholder="请输入终点经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="终点纬度" prop="field23">
              <el-input-number controls-position="right" v-model="form.field23" placeholder="请输入终点纬度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公路等级" prop="field9">
              <el-select v-model="form.field9" placeholder="请选择公路等级">
                <el-option label="三级" value="三级"></el-option>
                <el-option label="二级" value="二级"></el-option>
                <el-option label="一级" value="一级"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年日均交通等级" prop="field12">
              <el-select v-model="form.field12" placeholder="请选择级别">
                <el-option label="三级" value="三级"></el-option>
                <el-option label="二级" value="二级"></el-option>
                <el-option label="一级" value="一级"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理级别" prop="field13">
              <el-select v-model="form.field13" placeholder="请选择管理级别">
                <el-option label="三级" value="三级"></el-option>
                <el-option label="二级" value="二级"></el-option>
                <el-option label="一级" value="一级"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公路状态" prop="field19">
              <el-select v-model="form.field19" placeholder="请选择公路状态">
                <el-option label="启用" value="启用"></el-option>
                <el-option label="禁用" value="禁用"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设计单位" prop="field6">
              <el-input v-model="form.field6" placeholder="请输入设计单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="施工单位" prop="field7">
              <el-input v-model="form.field7" placeholder="请输入施工单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理单位" prop="field8">
              <el-input v-model="form.field8" placeholder="请输入管理单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护单位" prop="field15">
              <el-input v-model="form.field15" placeholder="请输入养护单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设计时速" prop="field10">
              <el-input v-model="form.field10" placeholder="请输入设计时速" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标线" prop="field14">
              <el-input v-model="form.field14" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="竣工日期" prop="field16">
              <el-date-picker
                clearable
                size="min"
                v-model="form.field16"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择竣工日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制表日期" prop="field18">
              <el-date-picker
                clearable
                size="small"
                v-model="form.field18"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择制表日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="制表单位" prop="field17">
              <el-input v-model="form.field17" placeholder="请输入制表单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGonglu,
  getGonglu,
  delGonglu,
  addGonglu,
  updateGonglu,
  exportGonglu,
} from "@/api/biz/gonglu";

export default {
  name: "Gonglu",
  dicts: ["shzt", "shzt"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公路信息表格数据
      gongluList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        field1: null,
        field2: null,
        field3: null,
        field4: null,
        field5: null,
        field6: null,
        field7: null,
        field8: null,
        field9: null,
        field10: null,
        field11: null,
        field12: null,
        field13: null,
        field14: null,
        field15: null,
        field16: null,
        field17: null,
        field18: null,
        field19: null,
        field20: null,
        field21: null,
        field22: null,
        field23: null,
        field24: null,
        field25: null,
        field26: null,
        field27: null,
        field28: null,
        field29: null,
        field30: null,
        type: null,
        num: null,
        status: null,
        date1: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        field1: [
          { required: true, message: "公路编码不能为空", trigger: "blur" },
        ],
        field2: [
          { required: true, message: "公路名称不能为空", trigger: "blur" },
        ],
        field3: [
          { required: true, message: "公路方向不能为空", trigger: "blur" },
        ],
        field4: [
          { required: true, message: "公路起点不能为空", trigger: "blur" },
        ],
        field5: [
          { required: true, message: "公路终点不能为空", trigger: "blur" },
        ],
        field6: [
          { required: true, message: "设计单位不能为空", trigger: "blur" },
        ],
        field7: [
          { required: true, message: "施工单位不能为空", trigger: "blur" },
        ],
        field9: [
          { required: true, message: "公路等级不能为空", trigger: "blur" },
        ],
        field20: [
          { required: true, message: "起点经度不能为空", trigger: "blur" },
        ],
        field21: [
          { required: true, message: "起点纬度不能为空", trigger: "change" },
        ],
        field22: [
          { required: true, message: "终点经度不能为空", trigger: "blur" },
        ],
        field23: [
          { required: true, message: "终点纬度不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.queryParams.field2 = this.$route.query.roadname
  },
  methods: {
    /** 查询公路信息列表 */
    getList() {
      this.loading = true;
      listGonglu(this.queryParams).then((response) => {
        this.gongluList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        field1: null,
        field2: null,
        field3: null,
        field4: null,
        field5: null,
        field6: null,
        field7: null,
        field8: null,
        field9: null,
        field10: null,
        field11: null,
        field12: null,
        field13: null,
        field14: null,
        field15: null,
        field16: null,
        field17: null,
        field18: null,
        field19: null,
        field20: null,
        field21: null,
        field22: null,
        field23: null,
        field24: null,
        field25: null,
        field26: null,
        field27: null,
        field28: null,
        field29: null,
        field30: null,
        type: null,
        num: null,
        status: "0",
        date1: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公路信息";
    },
    /** 编辑/查看按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGonglu(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑/查看公路信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGonglu(this.form).then((response) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGonglu(this.form).then((response) => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delGonglu(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有公路信息数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportGonglu(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
